// Exceptions.cpp
// (c) 2011, Charles Lechasseur
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "stdafx.h"
#include "Exceptions.h"


namespace RG64
{
    //
    // Default constructor. Calls GetLastError to get the error code.
    //
    Win32Exception::Win32Exception()
        : std::exception(),
          m_Message(),
          m_ErrorCode(::GetLastError())
    {
        Init();
    }

    //
    // Constructor with error code.
    //
    // @param p_ErrorCode Windows error code. If set, will retrieve the
    // error message from the system module.
    //
    Win32Exception::Win32Exception(const DWORD p_ErrorCode)
        : std::exception(),
          m_Message(),
          m_ErrorCode(p_ErrorCode)
    {
        Init();
    }

    //
    // Returns the exception description.
    //
    // @return Exception description.
    //
    const char* Win32Exception::what() const
    {
        return !m_Message.empty() ? m_Message.c_str()
                                  : std::exception::what();
    }

    //
    // Returns the Win32 error code passed to the constructor.
    //
    // @return Win32 error code.
    //
    DWORD Win32Exception::ErrorCode() const
    {
        return m_ErrorCode;
    }

    //
    // Initializes our message according to the error code
    // received by the constructor.
    //
    void Win32Exception::Init()
    {
        if (m_ErrorCode != NO_ERROR) {
            LPSTR pBuffer = 0;
            DWORD res = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                                         0,
                                         m_ErrorCode,
                                         0,
                                         reinterpret_cast<LPSTR>(&pBuffer),
                                         0,
                                         0);
            if (res != 0 && pBuffer != 0) {
                m_Message = (LPCSTR) ATL::CStringA(MAKEINTRESOURCEA(IDS_ERROR_PREFIX));
                m_Message.append(pBuffer, res);
                ::LocalFree(reinterpret_cast<HLOCAL>(pBuffer));
            }
        }
    }

} // namespace RG64
